// import type { oDialogPropsType } from './dialog.vue'
import dialogVue from './dialog.vue'
import {
  createApp,
  type Component,
  type ComponentOptionsMixin,
  nextTick
} from 'vue'

/**
 * 主动关闭调用返回的 close 方法
 * 主动验证validate，支持validate(['age']) 校验某些字段
 * 可清理验证 app.clearValidate(['age']) 可传某些字段或者不传就是全部
 * @param hook
 * @returns
 */
const open = function (hook: Function) {
  // dialogOptions: oDialogPropsType
  const app = createApp(dialogVue, {
    setup: hook,
    onUnmounted: () => {
      app.unmount()
    }
  })
  const div = document.createElement('div')
  document.body.appendChild(div)
  app.mount(div)
  return {
    app,
    close: app.unmount,
    validate: function (...args: any) {
      nextTick(function () {
        app._instance!.exposed!.validate(args)
      })
    },
    clearValidate: function (...args: any) {
      nextTick(function () {
        app._instance!.exposed!.clearValidate(args)
      })
    }
  }
}

export default {
  open
}
